package project.ccso.top.controller;

import java.io.IOException;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * BaseServlet
 * @author aibinxiao
 * @date 2018年8月23日 上午10:46:06
 */
@WebServlet("/BaseServlet")
public class BaseServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static Logger logger = LoggerFactory.getLogger(BaseServlet.class);

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		// 获取本次请求的完整url
		String url = request.getRequestURL().toString();
		// 获取本次请求的Servlet的名称
		String path = url.substring(url.lastIndexOf("/") + 1, url.length());
        try {
            //1、获得请求的method的名称
            String methodName = request.getParameter("methodName");
            logger.info("本次请求的Servlet为--" + path + "--请求此Servlet的方法为--" + methodName);
            //2、获得当前被访问的对象的字节码对象
            Class clazz = this.getClass();
            //3、获得当前字节码对象的中的指定方法
            Method method = clazz.getMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
            //4、执行相应功能方法
            method.invoke(this, request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
	}
       
}
